package org.lzuploader.gui;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.lzuploader.gui.listeners.CancelSelectionListener;
import org.lzuploader.gui.listeners.options.BrowseSelectionListener;
import org.lzuploader.gui.listeners.options.OkSelectionListener;
import org.lzuploader.model.Config;
import org.lzuploader.model.Localization;

/** Class used to display options dialog.
 * 
 * @author Emmanuel Cron
 *
 */
public final class WOptions
{
    /** Width of the window. */
    private static final int WINDOW_WIDTH = 300;
    /** Height of the window. */
    private static final int WINDOW_HEIGHT = 280;
    
    /** Logger of this class. */
    private static Log log = LogFactory.getLog(WOptions.class);
    /** Localization of this class. */
    private static Localization localization = Localization.getInstance();
    
    /** Shell used to display this window. */
    private Shell shell;
    
    /** Current configuration of the program. */ 
    private Config config = Config.getInstance();
    
    /* COMPONENTS */
    
    /** Combo containing possible program languages. */ 
    private Combo languageCombo;
    
    /** Text containing the path to World of Warcraft. */
    private Text wowPath;
    
    /* END OF COMPONENTS */
    
    /** Creates the options window.
     * Creates the options window, initializing its controls and various settings
     * @param parent    Parent that called the window.
     */
    public WOptions(final Shell parent)
    {
        log.debug("Creating options window.");
        
        shell = new Shell(parent, SWT.APPLICATION_MODAL + SWT.DIALOG_TRIM);
        shell.setData(this);
        
        Position.centerElement(parent, shell, WOptions.WINDOW_WIDTH, WOptions.WINDOW_HEIGHT);
        shell.setText(localization.getString("DIALOG_TITLE_OPTIONS"));
        
        // Populating window
        populateWindow();
        
        // Updating window
        refreshComponents();
        
        log.trace("Opening window.");
        
        shell.open();
    }
    
    /** Updates window components with several data from {@link Localization}, {@link Config}, ... */
    private void refreshComponents()
    {
        log.debug("Updating window components.");
        
        log.trace("Printing WoW path in text.");
        
        if (config.getWowPath() != null)
        {
            wowPath.setText(config.getWowPath());
        }
        
        log.trace("Filling combo.");
        
        languageCombo.removeAll();
        
        int currentLocale = 0;
        for (int i = 0 ; i < Localization.LOCALES.length ; i++)
        {
            languageCombo.add(Localization.LOCALES[i].getLiteral());
            
            if (Localization.LOCALES[i].getId().equals(config.getLocalization()))
            {
                currentLocale = i;
            }
        }
        
        log.trace("Selecting current locale in combo.");
        
        languageCombo.select(currentLocale);
    }
    
    /** Populates the options window with various controls.
     * Populates the options window with various controls.
     */
    private void populateWindow()
    {
        log.debug("Populating window.");
        
        log.trace("Creating groups.");
        
        final Group wow = new Group(shell, SWT.SHADOW_NONE);
        wow.setLocation(10, 10);
        wow.setSize(275, 60);
        wow.setText(localization.getString("GROUP_WOW_LOCATION"));
        
        final Group language = new Group(shell, SWT.SHADOW_NONE);
        language.setLocation(10, 150);
        language.setSize(275, 55);
        language.setText(localization.getString("GROUP_LANGUAGE"));
        
        final Group shortcuts = new Group(shell, SWT.SHADOW_NONE);
        shortcuts.setLocation(10, 80);
        shortcuts.setSize(275, 60);
        shortcuts.setText(localization.getString("GROUP_SHORTCUTS"));
        
        log.trace("Populating groups.");
        
        // World of Warcraft Location
        wowPath = new Text(wow, SWT.READ_ONLY + SWT.BORDER);
        wowPath.setLocation(10, 23);
        wowPath.setSize(175, 20);
        
        final Button wowBrowse = new Button(wow, SWT.PUSH);
        wowBrowse.setLocation(190, 20);
        wowBrowse.setSize(75, 25);
        wowBrowse.setText(localization.getString("BUTTON_BROWSE") + "...");
        wowBrowse.addSelectionListener(new BrowseSelectionListener());
        
        // Language
        languageCombo = new Combo(language, SWT.DROP_DOWN + SWT.READ_ONLY);
        languageCombo.setLocation(60, 20);
        languageCombo.setSize(150, 0);
        
        // Shortcuts
        final Button desktop = new Button(shortcuts, SWT.PUSH);
        desktop.setLocation(30, 20);
        desktop.setSize(100, 25);
        desktop.setText(localization.getString("BUTTON_DESKTOP"));
        desktop.setEnabled(false);
        //desktop.addSelectionListener(new DesktopShortcutSelectionListener());
        
        final Button startMenu = new Button(shortcuts, SWT.PUSH);
        startMenu.setLocation(145, 20);
        startMenu.setSize(100, 25);
        startMenu.setText(localization.getString("BUTTON_START_MENU"));
        startMenu.setEnabled(false);
        //startMenu.addSelectionListener(new StartMenuShortcutSelectionListener());
        
        log.trace("Creating last buttons.");
        
        final Button ok = new Button(shell, SWT.PUSH);
        ok.setText(localization.getString("BUTTON_OK"));
        ok.setSize(80, 25);
        ok.setLocation(115, 220);
        ok.addSelectionListener(new OkSelectionListener());
        
        final Button cancel = new Button(shell, SWT.PUSH);
        cancel.setText(localization.getString("BUTTON_CANCEL"));
        cancel.setSize(80, 25);
        cancel.setLocation(200, 220);
        cancel.addSelectionListener(new CancelSelectionListener());
    }
    
    /** Returns the wow path widget so that it can be changed.
     * @return The wow path widget.
     */
    public Text getWowPathWidget()
    {
        return wowPath;
    }
    
    /** Returns the language combo widget so that it can be changed.
     * @return The language combo widget.
     */
    public Combo getLanguageWidget()
    {
        return languageCombo;
    }
    
    /** Returns the shell associated with this window.
     * @return The shell associated to this window
     */
    public Shell getShell()
    {
        return shell;
    }
}
